﻿#include"unrealircd.h"
ModuleHeader MOD_HEADER
= {
	"third/mail",
	"1.1",
	"This module will send email",
	"waitch",
	"unrealircd-5",
};
char *sms_template="尊敬的用户:\n\n您的验证码为： %s\n\n验证码有效时间为5分钟\n\n如果您没有请求验证码操作，请忽略此邮件\n\n\tUEIM 团队";
CMD_FUNC(cmd_emailVerification);
void SendRegistSMS(Client *client ,char * email,int sendType);
void testSendSMS(Client *client ,char * email,int sendType);
MOD_TEST(){

	return MOD_SUCCESS;
}

MOD_INIT()
{
	CommandAdd(modinfo->handle, "EMAILVERIFICATION", cmd_emailVerification, 2, CMD_UNREGISTERED|CMD_UNLOGIN|CMD_USER);
	
	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}


/**
 * 发送邮箱验证码的命令
 * parv[1]:邮箱号码
 * parv[2]:验证领域
 */
CMD_FUNC(cmd_emailVerification){
	if (!IsAPPIdent(client)) {
		sendnumeric(client, ERR_WHITHOUTAPPIDENT);
		return;
	}
	if (parc != 3 || BadPtr(parv[2])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "EMAILVERIFICATION");
		return;
	}

	if(!is_digist_string(parv[2],strlen(parv[2]))){
		sendnumeric(client, PARAMERROR, "smsStatus","not right value");
		return;
	}
	int smsStatus=atoi(parv[2]);
	switch(smsStatus){
		case SMS_STATUS_REGISTER:
		case SMS_STATUS_LOGIN:
		case SMS_STATUS_DELETE:
		case SMS_STATUS_RESET_PASSWORD:
		break;
		default:
			sendnumeric(client, PARAMERROR, "smsStatus","not right value");
			return;
	}
	#ifndef DEBUG
		SendRegistSMS(client,parv[1],smsStatus);
	#else
		testSendSMS(client,parv[1],smsStatus);
	#endif
	
}

/**
 * 发送注册手机验证码
 * @param[in] email:邮箱号
 */
void SendRegistSMS(Client *client ,char * email,int sendType){
	//验证邮箱是否已经注册过了
	int emailnum=0;
	D_im_user_email  *users=queryImUserEmailByEmail(email,&emailnum);
	db_D_UserEmailFree(users,emailnum);
	switch(sendType){
		case SMS_STATUS_REGISTER:
			if(emailnum!=0){
				sendnumeric(client, EMAILEXITED, email);
				return;
			}
			break;
		case SMS_STATUS_LOGIN:
		case SMS_STATUS_DELETE:
		case SMS_STATUS_RESET_PASSWORD:
			if(emailnum!=1){
				sendnumeric(client, EMAILNOTEXITED, email);
				return;
			}
			break;
		default:
			sendnumeric(client, SMSSENDFAILE, email);
			return;
	}
	char *param=random_digist(6);
	char sendData[200]="";
	sprintf(sendData,sms_template,param);


	SmtpData *stmpdata=createSmtpData(sendData,email);

	stmp_addheadData(stmpdata,"Subject","UEIM 验证码");
	stmp_addheadData(stmpdata,"From","UEIM@temp.com");
	
	add_smtpReady(stmpdata);
	D_im_sms *sms_data=safe_alloc(sizeof(D_im_sms));
	sms_data->code=NULL;
	safe_strdup(sms_data->code,param);
	sms_data->contactvalue=NULL;
	safe_strdup(sms_data->contactvalue,email);
	sms_data->sms_channel=safe_alloc(sizeof(int));
	*(sms_data->sms_channel)=SMS_MODE_EMAIL;
	sms_data->sms_type=safe_alloc(sizeof(int));
	*(sms_data->sms_type)=sendType;
	sms_data->used=safe_alloc(sizeof(int));
	*(sms_data->used)=0;
	sms_data->valid_time=safe_alloc(sizeof(D_TimeStamp));
	D_getCurrectTime(sms_data->valid_time);
	int insertStatus=0;
	char insert_error_message[200]="";
	insertImSms(sms_data,&insertStatus,insert_error_message);
	if(insertStatus==-1){
		ircd_log(LOG_ERROR, "insert im_sms error:%s", insert_error_message);
		sendnumeric(client, SMSSENDFAILE, email);
		return;
	}
	else{
		sendnumeric(client, SMSSENDSUCCESS, email);
	}
}

void testSendSMS(Client *client ,char * email,int sendType){
	D_im_sms *sms_data=db_createImSMS();
	safe_strdup(sms_data->code,"111111");
	safe_strdup(sms_data->contactvalue,email);
	sms_data->sms_channel=safe_alloc(sizeof(int));
	*(sms_data->sms_channel)=SMS_MODE_EMAIL;
	sms_data->sms_type=safe_alloc(sizeof(int));
	*(sms_data->sms_type)=sendType;
	sms_data->used=safe_alloc(sizeof(int));
	*(sms_data->used)=0;
	sms_data->valid_time=safe_alloc(sizeof(D_TimeStamp));
	D_getCurrectTime(sms_data->valid_time);
	int insertStatus=0;
	char insert_error_message[200]="";
	D_im_sms *sms_in=insertImSms(sms_data,&insertStatus,insert_error_message);
	db_D_ImSMSFree(sms_data,1);
	db_D_ImSMSFree(sms_in,insertStatus);
	if(insertStatus==-1){
		ircd_log(LOG_ERROR, "insert im_sms error:%s", insert_error_message);
		return;
	}
	sendnumeric(client, SMSSENDSUCCESS, email);
}